module(...,package.seeall)

function new ()
    -- ===== Global
    _G.paused = false
    _G.levelEnd = false
	_G.currentMode = "classic"	
    
    -- ===== Basics
    local localGroup = display.newGroup()
    local levelGroup = display.newGroup()

    local levels = {}
    
    local transManager = require("transitionManager").new()
    
    local objects = require("game_objects")
    
    local score = require("score")
    local currentScore = 0
    
	-- ===== Your Score
    local scoreTitle = display.newImage( "images/digits/your_score.png" )
    scoreTitle.x = 70; scoreTitle.y = 25
    
	local yourScore = display.newGroup()
    yourScore:insert(display.newGroup())

    yourScore.x = 135; yourScore.y = 55
	--
    
    -- ===== Level Score
    local levelScoreTitle = display.newImage( "images/digits/level_score.png" )
    levelScoreTitle.x = _W - 70; levelScoreTitle.y = 25
    
    local levelScore = display.newGroup()
    levelScore:insert(display.newGroup())

    levelScore.x = 475; levelScore.y = 55
    --
 
    -- === Game Touch Logic
    local function smashEvent(e)
	
        local m = e.target
        if (e.phase == "ended") then
            if (not _G.paused) then
                m.vida = m.vida - 1;
                if (m.vida == 0) then
					currentScore = currentScore + m.point
                    if (currentScore < 0) then
                        currentScore = 0
                    end
                    score.updateScore("s", yourScore, currentScore)
					local somAnimal = audio.loadSound(m.sound)
					audio.play(somAnimal)
                    m:prepare("m_pow")
                    m:play("m_pow")
                end
            end
        end
		
    end

    ------
    
    ----======================= Levels ===========================----
    levels[1] = require("paris")
    levels[2] = require("savana")
    levels[3] = require("japan")
    
    -----====================== Carrega os levels =======================-----
    levelGroup:insert(levels[_G.currentLevel].new(smashEvent, transManager))
    
    -- ===== Sound 
	media.playSound(levels[_G.currentLevel].getSound(), true);
	--
	    
    -- ======================== Time e Level End Logic ====================-----
    local currentTime = levels[_G.currentLevel].getTime()
    
    local timeTitle = display.newImage( "images/digits/time.png" )
    timeTitle.x = _W/2; timeTitle.y = 25
    
    local timeLevel = display.newGroup()
    timeLevel:insert(display.newGroup())

    timeLevel.x = 295; timeLevel.y = 65
    timeLevel:scale(0.9,0.9)

    local function updateTime(e)
        -- Ajuste para transitions antecipados no final do tempo
        if (currentTime == 1) then
            _G.levelEnd = true
        end
        
        if (currentTime > 0 and not _G.paused) then
            currentTime = currentTime - 1
            score.updateScore("t", timeLevel, currentTime)
        elseif (currentTime == 0) then
            -- Retira a atualização
            timer.cancel(e.source)
            media.stopSound()
            -- Lógica de fim de level (próximo, ou verificar se é o último e terminar o jogo)
            if (currentScore >= levels[_G.currentLevel].getScore()) then
                -- Verifica se é o ultimo level do modo clássico
                if (_G.currentLevel < #levels) then
                    _G.currentLevel = _G.currentLevel + 1
                    Runtime:removeEventListener("enterFrame", objects.moveSky)
                    timer.performWithDelay(1500, function(e) objects.newTelaCheck("level", "classic", e.source); timer.cancel(e.source) end, 0)
                    --objects.newTelaCheck("level", "classic", e.source)
                else
                    _G.currentLevel = 1
                    Runtime:removeEventListener("enterFrame", objects.moveSky)
                   
                    
                    -- Volta ao menu
                    timer.performWithDelay(3500, function(e) objects.newTelaCheck("end", "menu", e.source); timer.cancel(e.source) end, 0)
                end
            else
                objects.newTelaCheck("gameover", "menu", e.source)
            end
        end
    end
    
    local upTimer = timer.performWithDelay(1000, updateTime, 0)
    --

    -- ===== Pause Button
    local pause_btn = display.newImage("images/pause/pause_button.png")
    pause_btn.x = 35; pause_btn.y = _H - 35
    
    local pauseElements = objects.newPauseElements(transManager, pause_btn, localGroup, "classic", upTimer)
    --
    
    -- Insere na cena o level atual
    localGroup:insert(levelGroup[1])
    --------------------------------------------------------------------------------
    
    score.updateScore("s", yourScore, currentScore)    
    score.updateScore("t", timeLevel, levels[_G.currentLevel].getTime())    
    score.updateScore("s", levelScore, levels[_G.currentLevel].getScore())
    
    localGroup:insert(scoreTitle)
    localGroup:insert(timeTitle)
    localGroup:insert(levelScoreTitle)
    localGroup:insert(yourScore)
    localGroup:insert(timeLevel)
    localGroup:insert(levelScore)
    localGroup:insert(pause_btn)
    
    localGroup:insert(pauseElements)
    
    return localGroup
end